package com.travel.server.manager.impl.command.user;

import com.travel.common.CommonConstant;
import com.travel.server.manager.dto.request.user.UpdateUserRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.UserDal;
import com.travel.server.manager.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/20 13:00
 * @Desc:
 */
@Component
@Slf4j
public class UpdateUserCommand extends AbstractCommand<UpdateUserRequest, EmptyResponse> {

    @Autowired
    private UserDal userDal;

    @Override
    protected void checkInput(UpdateUserRequest request) {

    }

    @Override
    public EmptyResponse handle(UpdateUserRequest request) {
        UserInfo user = getUserInfo();
        Date now = new Date();

        UserInfo userInfo;
        if(request.getId() == null){
            userInfo = new UserInfo();
            userInfo.setState(CommonConstant.YES);
            userInfo.setCreateBy(user.getName());
            userInfo.setCreateTime(now);
        }else{
            userInfo = userDal.selectById(request.getId());
        }
        userInfo.setUpdateTime(now);
        userInfo.setUpdateBy(user.getName());
        userInfo.setName(request.getName());
        userInfo.setMobile(request.getMobile());
        userInfo.setAccount(request.getAccount());
        userInfo.setPassword(request.getPassword());
        userInfo.setDepartmentId(request.getDepartmentId());
        userInfo.setEnable(request.getState());
        userInfo.setHeadUrl(request.getHeadUrl());
        userDal.insertOrUpdate(userInfo);

        return new EmptyResponse();
    }
}
